package ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.exportRFC;

import java.awt.Font;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JLabel;

import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.NewsMaker;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.database.DatabaseConnection;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.model.News;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.model.User;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.exportRFCPanel.ExportRFCPanel;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.exportRFCPanel.exportRFCPanelSubs.ExportNewsTable;

public class ActualiseNewsListThread extends Thread {

	private ExportNewsTable exportNewsTable;
	private DatabaseConnection connection;
	private User signedinUser;
	private JLabel progress, title;
	private ExportRFCPanel exportRFCPanel;

	public ActualiseNewsListThread(ExportRFCPanel exportRFCPanel) {

		this.exportRFCPanel = exportRFCPanel;
		this.exportNewsTable = exportRFCPanel.getFunctionPanel().getNewsPanel()
				.getExportNewsTable();
		this.signedinUser = exportRFCPanel.getMainframe().getSignedInUser();
		this.progress = exportRFCPanel.getFunctionPanel().getProgress();
		this.title = exportRFCPanel.getFunctionPanel().getNewsPanel()
				.getTitle();
	}

	@Override
	public void run() {
		this.connection = new DatabaseConnection();
		this.connection.connect();
		// Flackern verhindern
		// this.progress.setVisible(true);

		String query = "SELECT * FROM `cms_news` WHERE `" + News.TC_TO
				+ "` = '" + this.signedinUser.getFullName() + "' ORDER BY `"
				+ News.TC_RFC_ID + "` DESC";

		ResultSet rs = connection.query(query);

		ArrayList<News> newslist = new ArrayList<News>();
		int unknow = 0;
		int all = 0;
		int old = this.exportNewsTable.getRowCount();

		try {
			while (rs.next()) {
				NewsMaker maker = new NewsMaker();
				newslist.add(maker.makeNews(rs));
				all++;
				if (rs.getString(News.TC_KNOWN).equals("false")) {
					unknow++;
				}
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}

		// wenn die tabelle nicht das erste mal geladen wird;
		// funktioniert nicht wenn keine Nachrichten vorhanden sind XD
	
			if (old != 0) {// sonst kommt beim programmstart eine meldung
				if (all != old) {
					
				this.exportRFCPanel
						.getMainframe()
						.getTrayLoader()
						.createSysTrayMessage("Neue Nachricht",
								"Sie haben eine Neue Nachricht erhalten");
				}
			}

			this.exportNewsTable.setVisible(false);
			this.exportNewsTable.clearTable();

			for (News news : newslist) {
				this.exportNewsTable.addNewsinTable(news);
			}
			if (unknow == 0) {
				Font font = new Font("no", Font.PLAIN, 15);
				this.title.setText("Benachrichtigungen");
				this.title.setFont(font);
			} else {

				Font font = new Font("no", Font.BOLD, 15);
				this.title.setText("Benachrichtigungen (" + unknow + ")");
				this.title.setFont(font);
			}
		

		this.exportNewsTable.setVisible(true);

		this.connection.disconnect();
		this.progress.setVisible(false);
	}
}
